package org.frankkie.parcdroid;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import java.io.PrintStream;
import java.util.List;

/**
 * ParcDroid, MainActivity
 * Hier begint de App
 * @author frankkie
 */
public class MainActivity extends MapActivity {

  MapView mapView;
  public static boolean route_displayed = false;
  public static LocationManager locationManager;
  public static Location location;
  public static MapController mapController;
  List<Overlay> mapOverlays;
  Drawable drawable;
  OverlayDing itemizedOverlay;
  LocationListener deListener;
  OverlayDing mijnLokatieOverlay;
  public static Context deContext;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //System.out en System.err doorlinken naar de DDMS (logcat)
    System.setErr(new PrintStream(new SystemOut("System.err")));
    System.setOut(new PrintStream(new SystemOut("System.out")));
    System.out.println("ParcDroid.MainActivity");

    //
    deContext = getApplicationContext();
    this.setContentView(R.layout.main);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();

    //Kaart centreren boven de efteling
    GeoPoint stoomCaroeselPoint = OverlayDing.getPoint(51.651123, 5.048898);
    mapController.setCenter(stoomCaroeselPoint);
    mapController.setZoom(16);
    mapView.setSatellite(true);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    ////OVERLAY START
    mapOverlays = mapView.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.demarker50);
    itemizedOverlay = new OverlayDing(drawable, this);
    ////EINDE OVERLAY START

    ///Zet Markers
    OverlayItem stoomCaroeselItem = new OverlayItem(stoomCaroeselPoint, "StoomCaroesel", "De StoomCaroesel staat hier");
    itemizedOverlay.addOverlay(stoomCaroeselItem);
    ///Einde ZetMarkers

    /////ZOOI OVERLAY
    GeoPoint point = new GeoPoint(19240000, -99120000); //mexico
    OverlayItem overlayitem = new OverlayItem(point, "Mexico", "Mexico !");
    itemizedOverlay.addOverlay(overlayitem);
    GeoPoint point2 = new GeoPoint(35410000, 139460000); //japan
    OverlayItem overlayitem2 = new OverlayItem(point2, "Japan", "Japan !");
    itemizedOverlay.addOverlay(overlayitem2);
    /////EINDE ZOOI OVERLAY

    ///OVERLAY EIND
    OverlayDinges overlaydinges = new OverlayDinges();
    mapOverlays.add(overlaydinges);
    mapOverlays.add(itemizedOverlay);
    ///EINDE OVERLAY EIND

    ///////////////
    Drawable androidDrawable = this.getResources().getDrawable(R.drawable.androidmarker);
    mijnLokatieOverlay = new OverlayDing(androidDrawable, this);
    deLokatieIsVeranderd(location);
    mapOverlays.add(mijnLokatieOverlay);
    ///////////////

    ///
    deListener = new LocationListener() {

      public void onLocationChanged(Location arg0) {
        System.out.println("onLocationChanged");
        deLokatieIsVeranderd(arg0);
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        System.out.println("onStatusChanged");
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      public void onProviderEnabled(String arg0) {
        System.out.println("onProviderEnabled");
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      public void onProviderDisabled(String arg0) {
        System.out.println("onProviderDisabled");
        //throw new UnsupportedOperationException("Not supported yet.");
      }
    };
    long minTime = 1;
    float minDistance = 1;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, deListener);
  }

  @Override
  protected boolean isRouteDisplayed() {
    //throw new UnsupportedOperationException("Not supported yet.");
    return route_displayed;
  }

  @Override
  protected void onPause() {
    super.onPause();
    //Als de app wordt ge-pauze-erd
    //dan zorgen we dat hij de GPS-positie minder vaak refresh-t.
    //om de batterij te sparen.
    locationManager.removeUpdates(deListener);
    long minTime = 30;
    float minDistance = 5;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, deListener);
  }

  @Override
  protected void onResume() {
    super.onResume();
    //Als de app weer verder gaat
    //dan refreshen we de positie weer vaak.
    locationManager.removeUpdates(deListener);
    long minTime = 1;
    float minDistance = 1;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, deListener);
  }

  public void maakTosti(OverlayItem ding) {
    Toast.makeText(getApplicationContext(), "" + ding.getTitle() + "\n" + ding.getSnippet(), Toast.LENGTH_LONG).show();
  }

  public void deLokatieIsVeranderd(Location lo) {
    try {
      mijnLokatieOverlay.wisDeOverlays();
      //GeoPoint mijnPoint = OverlayDing.getPoint(lo.getLatitude(),lo.getLongitude());
      System.out.println("Lokatie: " + lo.getLatitude() + " " + lo.getLongitude());
      GeoPoint mijnPoint = new GeoPoint((int) (lo.getLatitude() * 1E6), (int) (lo.getLongitude() * 1E6));
      OverlayItem mijnLokatieItem = new OverlayItem(mijnPoint, "HIER", "Hier ben je !");
      mijnLokatieOverlay.addOverlay(mijnLokatieItem);
      //Proberen om meteen te repainten
      mapController.scrollBy(1, 1);
      mapController.scrollBy(-1, -1);
      boolean koekjes=false;
      if (koekjes) {
        mapController.animateTo(mijnPoint);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
